var barqrGen = require('../../../utils/wxbarqrGen.js')
var {formatDate} = require('../../../utils/util')
var allcoupon
var app = getApp()

Page({
  data: {
    couponlist: []
  },
  onLoad: function (options) {
    var cptype = '1'
    if (options.cptype) {
      cptype = options.cptype
    }
    this.setData({
      cptype: cptype
    })
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        var iconhgt = (res.windowWidth - 20) * 252 / 681
        that.setData({
          iconhgt: iconhgt
        })
      },
    })

    this.createBarcode()
    app.showLoading()
    this.loadCouponlist()
  },
  loadCouponlist: function () {
    var url = '/yuanmai_we/activity/getpromotion/list'
    app.get(url).then(res => {
      app.hideLoading()
      allcoupon = res
      for (var keystr in allcoupon) {
        var temp = ''
        for (var i = 0; i < allcoupon[keystr].length; i++) {
          temp = allcoupon[keystr][i].STARTTIME
          allcoupon[keystr][i].STARTTIME = temp.split(' ')[0]
          temp = allcoupon[keystr][i].ENDTIME
          allcoupon[keystr][i].ENDTIME = temp.split(' ')[0]
        }
      }
      this.dealCouponlist(this.data.cptype)
    })
  },
  dealCouponlist: function (cptype) {
    var temp_list = []
    var arr
    if (cptype == '1') {
      arr = allcoupon.EXPRESS
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].STATUS == "UNUSED") {
          temp_list.push(arr[i])
        }
      }
      arr = allcoupon.SKU
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].STATUS == "UNUSED") {
          temp_list.push(arr[i])
        }
      }
      this.setData({
        couponlist: temp_list
      })
    }
    else if (cptype == '2') {
      arr = allcoupon.OFFLINE
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].STATUS == "UNUSED") {
          temp_list.push(arr[i])
        }
      }
      this.setData({
        offlinelist: temp_list
      })
      for (var i = 0; i < temp_list.length; i++) {
        var barcodeid = "bq" + i
        barqrGen.barcode(barcodeid, app.globalData.cardNO, 560, 240)
      }
    }
    else if (cptype == '3') {
      arr = allcoupon
      for (var keystr in arr) {
        for (var i = 0; i < arr[keystr].length; i++) {
          if (arr[keystr][i].STATUS == 'EXPIRED') {
            arr[keystr][i].ctype = keystr
            if (keystr == 'SKU') {
              arr[keystr][i].ctype = 'EXPRESS'
            }
            temp_list.push(arr[keystr][i])
          }
        }
      }
      this.setData({
        couponlist: temp_list
      })
    }
    else if (cptype == '4') {
      arr = allcoupon
      for (var keystr in arr) {
        for (var i = 0; i < arr[keystr].length; i++) {
          if (arr[keystr][i].STATUS == 'USED') {
            arr[keystr][i].ctype = keystr
            if (keystr == 'SKU') {
              arr[keystr][i].ctype = 'EXPRESS'
            }
            temp_list.push(arr[keystr][i])
          }
        }
      }
      this.setData({
        couponlist: temp_list
      })
    }
  },

  changeCouponType: function (e) {
    var cptype = e.currentTarget.id
    if (cptype == this.data.cptype) {
      return
    }
    this.setData({
      cptype: cptype,
      couponlist: []
    })
    this.dealCouponlist(this.data.cptype)
  },
  createBarcode: function () {
    var cardno = app.globalData.cardNO
    this.setData({
      cardNo: cardno
    })
  },
  useOfflineCoupon: function (e) {
    var curdate = formatDate(new Date, 'yyyy-MM-dd')
    var msg = '温馨提示：您的优惠券在' + curdate + '使用，确定要兑换吗？'
    var that = this
    app.confirm(msg, '确定', '取消').then(confirm => {
      if (confirm) {
        var couponid = e.currentTarget.dataset.couponid
        that.useOfflineCouponAction(couponid)
      }
    })
  },
  useOfflineCouponAction: function (couponid) {
    var url = '/yuanmai_we/activity/offline/usePromotion'
    var property = {
      ID: couponid
    }
    var params = {
      datas: JSON.stringify(property),
      __zaofans: true
    }
    app.request(url, params).then(res => {
      if (res.result === 'SUCCESS') {
        app.toast.success('兑换成功')
        app.getUserInfo()
        setTimeout(() => {
          app.globalData.needUpdateUserInfo = true
          wx.navigateBack()
        }, 1000)
      }
    })
  }
})
